package cn.edu.cjw.APMFP.ui;

import cn.edu.cjw.APMFP.RoleRight.pojo.RoleRight;
import cn.edu.cjw.APMFP.Util.CacheUtil;
import cn.edu.cjw.APMFP.Util.ImgUtil;
import cn.edu.cjw.APMFP.ui.component.RoundBorder;
import cn.edu.cjw.APMFP.user.pojo.User;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.IntStream;

/**
 * @package cn.edu.cjw.APMFP.ui
 * @className TopPanel
 * @note 头部面板
 * @author wind
 * @date 2022/3/10 20:03
 */
public class TopPanel extends JPanel {

    private MainFrame mainFrame;

    private final JLabel headLabel = new JLabel();

    private final JPanel headPanel = new JPanel();
    private final JPanel menuPanel = new JPanel();

    private final JLabel label1 = new JLabel("个人信息管理");
    private final JLabel label2 = new JLabel("生产任务管理");
    private final JLabel label3 = new JLabel("成品物料管理");
    private final JLabel label4 = new JLabel("员工账号管理");
//    private final JLabel label5 = new JLabel("审核");
    private final List<JLabel> labelList = new ArrayList<>();

    public TopPanel(MainFrame mainFrame){
        this.mainFrame = mainFrame;
        this.setLayout(new BorderLayout(10, 10));
        Image image = ImgUtil.getImage("/head.jpg");
        ImageIcon imageIcon = new ImageIcon(image.getScaledInstance(110, 110, Image.SCALE_SMOOTH));
        headLabel.setIcon(imageIcon);
        headPanel.setPreferredSize(new Dimension(120, 120));
        headPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        headPanel.add(headLabel);
        headPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
        this.add(headPanel, BorderLayout.WEST);

        menuPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 40));
        labelList.add(label1);
        labelList.add(label2);
        labelList.add(label3);
        labelList.add(label4);
//        labelList.add(label5);
        IntStream.range(0, labelList.size()).forEach(i -> {
            JLabel label = labelList.get(i);
            menuPanel.add(label);
            setLabelStyle(label, false);
            label.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    labelList.forEach(l -> {
                        setLabelStyle(l, false);
                    });
                    setLabelStyle(label, true);
                    User user = CacheUtil.getUser();
                    if(!"admin".equals(user.getAccount())){
                        RoleRight roleRight = user.getuRole();
                        ArrayList<String> arr = roleRight.getRRight();
                        if(i == 1 && arr.stream().noneMatch(s -> s.contains("生产任务管理"))){
                            JOptionPane.showMessageDialog(null, "无权限访问");
                            return;
                        }
                        if(i == 2 && arr.stream().noneMatch(s -> s.contains("成品物料管理"))){
                            JOptionPane.showMessageDialog(null, "无权限访问");
                            return;
                        }
                        if(i == 3 && arr.stream().noneMatch(s -> s.contains("员工账号管理"))){
                            JOptionPane.showMessageDialog(null, "无权限访问");
                            return;
                        }
                    }
                    switch (i){
                        case 0: mainFrame.centerPanel.setMenu1();break;
                        case 1: mainFrame.centerPanel.setMenu2();break;
                        case 2: mainFrame.centerPanel.setMenu3();break;
                        case 3: mainFrame.centerPanel.setMenu4();break;
//                        case 4: mainFrame.centerPanel.setMenu5();break;
                    }
                }
            });
        });
        setLabelStyle(label1, true);
        this.add(menuPanel, BorderLayout.CENTER);
        menuPanel.setPreferredSize(new Dimension(600, 120));
        menuPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
        this.setPreferredSize(new Dimension(900, 120));
    }

    /**
     * 设置label样式
     * @param label
     */
    public void setLabelStyle(JLabel label, boolean isSelected){
        label.setFont(new Font("宋体", Font.BOLD, 20));
//        label.setPreferredSize(new Dimension(150, 30));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setOpaque(true);
        if(isSelected){
            label.setBackground(Color.orange);
        }else{
            label.setBackground(this.getBackground());
        }
    }
}
